﻿using blackhouse.NemoBlog.Business;
using blackhouse.NemoBlog.Business.Model;
using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace blackhouse.StareKino.Web.Events {
    public class CinemaPostModel : IPostModel {

        #region Fields

        private NemoBlogPost post;
        private NemoBlogSettings settings;
        private int constPostId = -1;

        #endregion

        #region Properties

        private int PostId {
            get {
                if (constPostId == -1) {
                    string sPostId = HttpContext.Current.Request["post"];
                    int postId = 0;
                    if (!String.IsNullOrEmpty(sPostId)
                        && Int32.TryParse(sPostId, out postId))
                        this.constPostId = postId;
                    else
                        this.constPostId = 0;
                }
                return this.constPostId;
            }
        }

        public NemoBlogPost Post {
            get {
                if (this.post == null) {
                    if (HttpContext.Current != null) {
                        Page p = HttpContext.Current.Handler as Page;
                        if (p != null && p.Items.Contains("CurrentPost"))
                            this.post = p.Items["CurrentPost"] as NemoBlogPost;
                        if (this.post == null) {
                            if (this.PostId > 0) {
                                NemoBlogPost tPost = new NemoBlogPost(this.PostId);
                                if (p != null && tPost != null && tPost.Exists) {
                                    this.post = tPost;
                                    p.Items.Add("CurrentPost", this.post);
                                }
                            }
                        }
                    }
                }
                return this.post;
            }
        }

        public NemoBlogSettings Settings {
            get {
                if (this.settings == null && this.Post != null) {
                    Page p = HttpContext.Current.Handler as Page;
                    if (p != null && p.Items.Contains("CurrentPostSettings"))
                        this.settings = p.Items["CurrentPostSettings"] as NemoBlogSettings;
                    if (this.settings == null) {
                        this.settings = new NemoBlogSettings(this.Post.ModuleId);
                        if (p != null && this.settings != null)
                            p.Items.Add("CurrentPostSettings", this.settings);
                    }
                }
                return this.settings;
            }
        }

        #endregion

        #region Constructors

        public CinemaPostModel() : this(-1) { }

        public CinemaPostModel(int postId) {
            this.constPostId = postId;
        }

        #endregion

        #region Methods

        public NemoBlogPost[] GetSimilarPosts() {
            if (this.Settings.UseSimilarPostsSystem) {
                IGroup group = NemoBlogPosts.BelongToGroup();
                GroupRecords records = new GroupRecords(group);
                GroupRecordQuery q = new GroupRecordQuery(group);
                q.SetProcedureName("bhp_NemoBlogPosts_GetSimilarEvents");
                q.AddProcedureParameter("PostId", this.Post.PostId, DbType.Int32);
                q.AddProcedureParameter("Count", this.Settings.SimilarPostsCount, DbType.Int32);
                q.AddProcedureParameter("PastDays", this.Settings.SimilarPostsDayAgo, DbType.Int32);
                q.AddProcedureParameter("ForwardDays", this.Settings.SimilarPostsForwardDays, DbType.Int32);
                q.AddProcedureParameter("Language", CultureInfo.CurrentCulture.Name, DbType.AnsiStringFixedLength);
                return records.GetRecords<NemoBlogPost>(q);
            }
            else
                return new NemoBlogPost[0];
        }

        #endregion

    }
}